package me.loque;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Returns the map of messages for the specified locale.
 * <P/>
 * Created on 25 Sep 2009, at 07:52:03.
 * Project: LoQueMe.
 * Filename: Messages.java.
 * &copy; Copyright <A href="http://www.loque.me">LoQueMe</A> 25 Sep 2009
 * 
 * @author joel
 */
public abstract class Messages
{
    /**
     * Messages bundle class name.
     */
    private static final String MESSAGES_BUNDLE = "messages.MessagesBundle";

    /**
     * Gets a map of key:value string pairs of the messages for the specified locale.
     * 
     * @param language The language.
     * 
     * @return The map of messages.
     */
    public static Response getMessagesMap(String language)
    {
        // Create locale.
        Locale locale = new Locale(language);
        
        // Load messages bundle.
        ResourceBundle messagesBundle = ResourceBundle.getBundle(MESSAGES_BUNDLE, locale);
        messagesBundle.getLocale().getCountry();
        
        // Create map of key : value pairs.
        Map<String, String> map = convertBundleToMap(messagesBundle);
        
        // Create and return response object.
        return new Response(language.equals("en") ? false : messagesBundle.getLocale().getLanguage().isEmpty(), map);
    }
    
    /**
     * Converts a bundle to a map.
     * 
     * @param bundle The bundle.
     * 
     * @return The map.
     */
    private static Map<String, String> convertBundleToMap(ResourceBundle bundle)
    {
        Map<String, String> map = new HashMap<String, String>();
        String value;
        for (String key : bundle.keySet())
        {
            value = bundle.getString(key);
            map.put(key, value);
        }
        
        return map;
    }
    
    /**
     * Class to hold response from message lookup.
     * <P/>
     * Created on 28 Sep 2009, at 08:50:50.
     * Project: LoQueMe.
     * Filename: Messages.java.
     * &copy; Copyright <A href="http://www.loque.me">LoQueMe</A> 28 Sep 2009
     * 
     * @author joel
     */
    public static class Response
    {
        /**
         * Whether or not the default bundle was used because there was no available translation.
         */
        private final boolean noTranslation;
        
        /**
         * The message map.
         */
        private final Map<String, String> messages;
        
        /**
         * Constructor.
         * 
         * @param noTranslation Whether or not the default bundle was used because there was no available translation.
         * @param map The message map.
         */
        public Response(boolean noTranslation, Map<String, String> map)
        {
            this.noTranslation = noTranslation;
            this.messages = map;
        }

        /**
         * Gets the defaultBundle.
         *
         * @return Returns the defaultBundle.
         */
        public boolean isNoTranslation()
        {
            return noTranslation;
        }

        /**
         * Gets the messages map.
         *
         * @return Returns the messages map.
         */
        public Map<String, String> getMessages()
        {
            return messages;
        }
    }
}
